<!-- NProgress -->
<link href="/vendors/nprogress/nprogress.css" rel="stylesheet" />
<!-- 上传图片 -->
<link href="/vendors/fileinput/fileinput.min.css" rel="stylesheet" />

<!-- page content -->
<div id="profileDiv" class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<h3>User Profile</h3>
			</div>

			<div class="title_right">
				<div class="col-md-5 col-sm-5 form-group pull-right top_search">
					<div class="input-group">
						<input type="text" class="form-control" placeholder="Search for..." />
						<span class="input-group-btn">
							<button class="btn btn-secondary" type="button">Go!</button>
						</span>
					</div>
				</div>
			</div>
		</div>

		<div class="clearfix"></div>

		<div class="row">
			<div class="col-md-12 col-sm-12">
				<div class="x_panel">
					<div class="x_title">
						<h2>User Report <small>Activity report</small></h2>
						<ul class="nav navbar-right panel_toolbox">
							<li>
								<a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
							</li>
							<li class="dropdown">
								<a
										href="#"
										class="dropdown-toggle"
										data-toggle="dropdown"
										role="button"
										aria-expanded="false"
								><i class="fa fa-wrench"></i
								></a>
								<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
									<a class="dropdown-item" href="#">Settings 1</a>
									<a class="dropdown-item" href="#">Settings 2</a>
								</div>
							</li>
							<li>
								<a class="close-link"><i class="fa fa-close"></i></a>
							</li>
						</ul>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<div class="col-md-3 col-sm-3 profile_left">
							<div class="profile_img">
								<div id="crop-avatar">
									<!-- Current avatar -->

									<img
											class="img-responsive avatar-view"
											:src="user.userImage"
											alt="Avatar"
											title="Change the avatar"
									/>
								</div>
							</div>
							<h3>{{user.userName}}</h3>

							<ul class="list-unstyled user_data">
								<li>
									<i class="fa fa-map-marker user-profile-icon"></i> San Francisco, California, USA
								</li>

								<li><i class="fa fa-briefcase user-profile-icon"></i> Software Engineer</li>

								<li class="m-top-xs">
									<i class="fa fa-external-link user-profile-icon"></i>
									<a href="http://www.kimlabs.com/profile/" target="_blank">www.kimlabs.com</a>
								</li>
							</ul>
							<a data-toggle="modal" data-target="#profileEdit" class="btn btn-success"
							><i class="fa fa-edit m-right-xs"></i>Edit Profile</a
							>
							<br />
						</div>
						<div class="col-md-9 col-sm-9">
							<div class="profile_title">
								<div class="col-md-6">
									<h2>User Activity Report</h2>
								</div>
								<div class="col-md-6">
									<div
											id="reportrange"
											class="pull-right"
											style="margin-top: 5px; cursor: pointer; padding: 5px 10px"
									></div>
								</div>
							</div>
							<!-- start of user-activity-graph -->
							<div id="graph_bar" style="width: 100%; height: 280px"></div>
							<!-- end of user-activity-graph -->
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div th:replace="account/profileEdit"></div>
	<!-- /page content -->
</div>

<!-- NProgress -->
<script src="/vendors/nprogress/nprogress.js" type="text/javascript"></script>
<!-- morris.js -->
<script src="/vendors/raphael/raphael.min.js" type="text/javascript"></script>
<script src="/vendors/morris.js/morris.min.js" type="text/javascript"></script>
<!-- bootstrap-progressbar -->
<script src="/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js" type="text/javascript"></script>
<!-- 上传图片 -->
<script src="/vendors/fileinput/fileinput.min.js" type="text/javascript"></script>

<script>
	var profileVue = new Vue({
		el: "#profileDiv",
		data: {
			user: {
				userName: "",
				email: "",
				userImage: "",
				imageType: "profile-big",
			},
			imageTypes: [],
		},
		methods: {
			initProfile: function () {
				var userId = $("#userId").val();
				var self = this;
				$.ajax({
					url: "/api/user/" + userId,
					type: "get",
					// contentType: "application/json",
					// data: JSON.stringify(searchBean),
					success: function (rs) {
						self.user = rs;
						if (rs.userImage == "" || rs.userImage == null) {
							self.user.userImage = "/images/picture.jpg";
						}
						self.user.imageType = "profile-big";
					},
					error: function (data) {
						layer.alert(data.responseText, { icon: 0 });
					},
				});
			},
			initImageTypes: function () {
				var self = this;
				$.ajax({
					url: "/api/dictionary/imageType",
					type: "get",
					// contentType: "application/json",
					// data: JSON.stringify(searchBean),
					success: function (rs) {
						self.imageTypes = rs;
					},
					error: function (data) {
						layer.alert(data.responseText, { icon: 0 });
					},
				});
			},
			uploadImage: function () {
				var self = this;
				$("#uploadImage")
						.fileinput({
							uploadUrl: "/api/image/" + self.user.imageType,
							enctype: "multipart/form-data",
							previewFileType: "image",
							uploadAsync: true,
							allowedFileExtensions: ["png", "jpg", "jpeg", "ico", "bmp", "gif"],
							maxFileCount: 1,
							maxFileSize: $("[name='imageTypes']").find("option:selected").attr("size"),
							minImageWidth: 10,
							minImageHeight: 10,
							maxImageWidth: $("[name='imageTypes']").find("option:selected").attr("length"),
							maxImageHeight: $("[name='imageTypes']").find("option:selected").attr("height"),
							msgFilesTooMany: "Upload file count({n} - {m})",
							showCaption: false,
							dropZoneEnabled: false,
							showBrowse: true,
							browseClass: "btn btn-primary",
							uploadClass: "btn btn-info",
							removeClass: "btn btn-danger",
						})
						.on("fileerror", function (event, data, msg) {
							layer.alert("Upload file failed" + msg, { icon: 0 });
						})
						.on("fileuploaded", function (event, data) {
							if (data.response.status == 200) {
								self.user.userImage = data.response.object;
							} else {
								$(".fileinput-remove-button").click();
							}
							layer.alert(data.response.message, { icon: 0 });
						})
						.on("fileclear", function (event) {});
			},
			refreshFileInput: function () {
				$("#uploadImage").fileinput("refresh", {
					maxFileSize: $("[name='imageTypes']").find("option:selected").attr("size"),
					maxImageWidth: $("[name='imageTypes']").find("option:selected").attr("length"),
					maxImageHeight: $("[name='imageTypes']").find("option:selected").attr("height"),
				});
			},
			updateModel: function () {
				if (this.user.userName == "" || this.user.email == "") {
					layer.alert("Please input user info.", { icon: 0 });
					return;
				}

				console.log(this.user);

				var self = this;
				$.ajax({
					url: "/api/user",
					type: "put",
					contentType: "application/json",
					data: JSON.stringify(self.user),
					success: function (rs) {
						if (rs.status == 200) {
							$("[name=userImage]").attr("src", self.user.userImage);
							$("[name=userName]").html(self.user.userName);
							$("#profileEdit").modal("hide");
						} else {
							layer.alert(rs.message, { icon: 0 });
						}
					},
					error: function (data) {
						layer.alert(data.responseText, { icon: 0 });
					},
				});
			},
		},
		mounted: function () {
			this.initProfile();
			this.initImageTypes();
			this.uploadImage();
		},
		updated: function () {
			this.refreshFileInput();
		},
	});
</script>
